<?php 
/****

	If you want the best of the world,
give the world your best anytime！
            				——不二
                							
****/


//判断常量是否定义,如果没定义，意味着不是请求的index.php
defined('ACCESS')||exit('Forbidden!');

//初始化类

class Application {
	//初始化字符集
	private static function setHeader(){
		header('content-type:text/html;chartset=utf8');
	}

	//初始化系统常量
	private static function setConst(){
		//设置根目录常量
		define('ROOT_DIR', dirname(__DIR__));
		//设置其他目录常量
		define('CORE_DIR',   ROOT_DIR . '/Core');
		define('ACTION_DIR', ROOT_DIR . '/Action');
		define('CONF_DIR',   ROOT_DIR . '/Conf');
		define('MODEL_DIR',  ROOT_DIR . '/Model');
		define('VIEW_DIR',   ROOT_DIR . '/View');
		define('PUB_DIR',    ROOT_DIR . '/Public');
		define('LIB_DIR',    ROOT_DIR . '/Lib');
	}

	//定义错误信息
	private static function setErrors(){
		@ini_set('error_reporting', E_ALL | E_STRICT);
		@ini_set('display_errors', 1);
	}

	//自动加载类
	public static function setLoad($class){
		if(is_file(CORE_DIR."/$class.class.php")){
			require_once(CORE_DIR."/$class.class.php");
		}else if(is_file(MODEL_DIR."/$class.class.php")){
			require_once(MODEL_DIR."/$class.class.php");
		}else if(is_file(ACTION_DIR."/".GROUP."/$class.class.php")){
			require_once(ACTION_DIR."/".GROUP."/$class.class.php");
		}else {
			//说明不存在,
			//header('Location:index.php');

			//echo "<script>alert('引用非法类$class');location='index.php';</script>";
		}
	}

	//开启session
	private static function setSession(){
		@session_start();
	}

	//加载配置文件
	private static function setConfig(){
		$GLOBALS['config'] = require_once(CONF_DIR.'/config.php');
	}

	//初始化URL
	private static function setUrl(){
		//获取用户想要访问的module和action
		$group = isset($_REQUEST['group']) ? $_REQUEST['group'] : 'Home';
		$module = isset($_REQUEST['module']) ? $_REQUEST['module'] : 'Index';
		$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'index';

		//转为小写
		$group = strtolower($group);
		$module = strtolower($module);
		$action = strtolower($action);

		//类首字母大写
		$group = ucfirst($group);
		$module = ucfirst($module);

		//定义为系统常量
		define('GROUP', $group);
		define('MODULE', $module);
		define('ACTION', $action);
	}

	//权限验证
	private static function setPrivilege(){
		//放行部分不需要验证的控制器的方法
		if(!((MODULE == 'Privilege' && (ACTION=='login'||ACTION=='captcha'||ACTION=='checks')) || GROUP=='Home')) {
			//说明都需要验证
			$username = isset($_SESSION['username'])?$_SESSION['username']:'';
			$password = isset($_SESSION['password'])?$_SESSION['password']:'';
			if ( $username !== md5($GLOBALS['config']['username']) || $password !== md5($GLOBALS['config']['password']) ) {
				//未登录或者伪造session
				header('Location:index.php');
			}
		}
	}

	//分发
	private static function setDispatch(){
		//找到对应的控制器类，并进行实例化，调用方法
		$module = MODULE.'Action';
		$module = new $module();
		$action = ACTION;
		$module->$action();
	}





	public static function run(){
		//1.初始化字符集
		self::setHeader();
		//2.初始化系统常量
		self::setConst();
		//3.错误信息
		self::setErrors();
		//4.自动加载
		spl_autoload_register(array('Application','setLoad'));
		//5.开启session
		self::setSession();
		//6.加载配置文件
		self::setConfig();
		//7.初始化URL
		self::setUrl();
		//8.权限验证
		self::setPrivilege();
		//9.分发
		self::setDispatch();

	}
}

